<template>
  <div v-loading="pageLoading">
    <z-form :data="currentMo" :formProps="formProps" :fields="fields"></z-form>
    <el-form label-width="140px">
      <el-form-item label="已绑定机台">
        <z-table :list="boundList" :tableProps="tableProps" :columns="columns"></z-table>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import commonMixin from '@/views/_common/mixin'

export default {
  name: 'tab1',
  mixins: [commonMixin],
  props: {
    mo_no: {
      type: String,
      default: '',
    },
  },
  watch: {
    mo_no: {
      handler() {
        this.refreshData()
      },
    },
  },
  data() {
    return {
      api: this.$api.mo,
      query: {
        mo_no: null,
      },
      currentMo: {},
      formProps: {
        labelWidth: '140px',
        disabled: true,
      },
      fields: [
        { title: '工单编号', key: 'mo_no', span: 8 },
        { title: '工单类型', key: 'mo_type', span: 8 },
        { title: '投入线别', key: 'line_name', span: 8 },
        { title: '产品料号', key: 'key_part_no', span: 8 },
        { title: '客户代码', key: 'cust_code', span: 8 },
        { title: '客户料号', key: 'cust_pn_code', span: 8 },
        { title: 'BOM编码(版本)', key: 'bom_no_text', span: 16 },
        { title: '工单状态', key: 'mo_status_name', span: 8 },
        { title: '工艺流程(版本)', key: 'route_code_name', span: 24 },
        { title: '计划时间', key: 'mo_plan_time', span: 8 },
        { title: '目标产出时间', key: 'mo_target_time', span: 8 },
        { title: '创建时间', key: 'mo_create_time', span: 8 },
        { title: '计划数量', key: 'target_qty', span: 8 },
        { title: '已投入数量', key: 'input_qty', span: 8 },
        { title: '已产出数量', key: 'output_qty', span: 8 },
        { title: '备注', key: 'remarks', span: 24, name: 'textarea' },
      ],
      moStatus: { 0: '未上线', 1: '已上线', 2: '生产中', 3: '正常结单', 4: '手工结单' },
      boundList: [],
      tableProps: { opsColWith: '190px', border: true, highlightCurrentRow: true, hideOperations: true },
      columns: [
        { title: '机台编号', key: 'machine_code' },
        { title: '机台名称', key: 'machine_name' },
        { title: '模具编号', key: 'tooling_code' },
        { title: '模具名称', key: 'tooling_name' },
      ],
    }
  },
  methods: {
    refreshData() {
      if (this.mo_no) {
        this.query.mo_no = this.mo_no
        this.getInfo()
        this.getBoundInfo()
      } else {
        this.query.mo_no = null
        this.currentMo = {}
        this.boundList = []
      }
    },
    getBoundInfo() {
      this.pageLoading = true
      this.$request(this.$api.mo + 'getMoBoundInfo', {
        mo_no: this.query.mo_no,
      })
        .then((r) => {
          this.pageLoading = false
          this.boundList = r
        })
        .catch(() => {
          this.pageLoading = false
        })
    },
    // 获取基本信息
    getInfo() {
      this.$request(this.$api.mo + 'getinfo', {
        idOrNo: this.query.mo_no,
      }).then((v) => {
        if (v) {
          v.mo_status_name = this.moStatus[v['mo_status']]
          v.route_code_name = v.route_code + ' ' + v.route_name + ' (' + v.version + ')'
          v.bom_no_text = v.bom_no + ' (' + v.bom_version + ')'
          v.mo_create_time = v.mo_create_time ? v.mo_create_time.substring(0, 10) : ''
          v.mo_plan_time = v.mo_plan_time ? v.mo_plan_time.substring(0, 10) : ''
          v.mo_target_time = v.mo_target_time ? v.mo_target_time.substring(0, 10) : ''
          this.currentMo = Object.assign({}, this.currentMo, v)
          this.$emit('after-getmo', {
            bom_id: v.bom_id,
            mo_no: v.mo_no,
            route_id: v.route_id,
          })
        } else {
          this.currentMo = {}
          this.boundList = []
          this.$message.error('没有找到有效工单信息')
          this.$emit('after-getmo', {})
        }
      })
    },
  },
  created: function () {
    this.refreshData()
  },
}
</script>

<style lang="scss" scoped>
</style>